﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using BussinessLogic;
using WebClasses.Helpers;

namespace WebClasses.Pages
{
    public abstract class NormalPage : System.Web.UI.Page
    {
        public NormalPage()
        {
            this.EnableViewState = true;
        }

        #region GetParam methods

        /// <summary>
        /// Get a string value from either query string or form collection as required.
        /// Will trim the value.
        /// If there is no value (or it's empty, after trimming) the default is returned.
        /// If required then an exception is thrown if missing.
        /// </summary>
        /// <param name="name">arg to get</param>
        /// <param name="queryString">true if from query string, else form</param>
        /// <param name="required">true if required (will throw if not present)</param>
        /// <param name="defaultValue">Default to use if not present</param>
        /// <returns>string value</returns>
        public string GetParam(string name, bool queryString, bool required, string defaultValue)
        {
            return WebUtilities.GetParam(this.Context,name, queryString, required, defaultValue);
        }

        /// <summary>
        /// Get a string value from either query string or form collection as required.
        /// Will trim the value.
        /// If there is no value (or it's empty, after trimming) the default is returned.
        /// If required then an exception is thrown if missing.
        /// </summary>
        /// <param name="name">arg to get</param>
        /// <param name="queryString">true if from query string, else form</param>
        /// <param name="required">true if required (will throw if not present)</param>
        /// <param name="cleansed">True if string needs to be cleaned.</param>
        /// <param name="defaultValue">Default to use if not present</param>
        /// <returns>string value</returns>
        public string GetParam(string name, bool queryString, bool required, bool cleansed, string defaultValue)
        {
            return WebUtilities.GetParam(this.Context, name, queryString, required, defaultValue);
        }

        /// <summary>
        /// Get an integer value from the qs/form
        /// If there is no value (or it's empty, after trimming) the default is returned.
        /// If required then an exception is thrown if missing.
        /// </summary>
        /// <param name="name">arg to get</param>
        /// <param name="queryString">true if from query string, else form</param>
        /// <param name="required">true if required (will throw if not present)</param>
        /// <param name="defaultValue">Default to use if not present</param>
        /// <returns>int value</returns>
        public int GetParam(string name, bool queryString, bool required, int defaultValue)
        {
            return WebUtilities.GetParam(this.Context, name, queryString, required, defaultValue);
        }

        /// <summary>
        /// Get a long value from the qs/form
        /// If there is no value (or it's empty, after trimming) the default is returned.
        /// If required then an exception is thrown if missing.
        /// </summary>
        /// <param name="name">arg to get</param>
        /// <param name="queryString">true if from query string, else form</param>
        /// <param name="required">true if required (will throw if not present)</param>
        /// <param name="defaultValue">Default to use if not present</param>
        /// <returns>int value</returns>
        public long GetParam(string name, bool queryString, bool required, long defaultValue)
        {
            return WebUtilities.GetParam(this.Context, name, queryString, required, defaultValue);
        }

        #endregion

        public LogicController LogicController
        {
            get
            {
                return LogicController.Instance;
            }
        }

        //Encode之后再将"'"替换成"&#39"
        public string HtmlEncode(object o)
        {
            return HttpUtility.HtmlEncode(o.ToString()).Replace("'", "&#39;");
        }

        public string gb2utf(object str)
        {
            if (str == null)
            {
                return "";
            }
            string tempStr = str.ToString();
            string outStr = "";
            for (int i = 0; i < tempStr.Length; i++)
            {
                outStr += "&#x" + ((int)tempStr[i]).ToString("x").ToUpper() + ";";
            }
            return outStr;
        }
    }
}
